package com.leyou.search.listener;

import com.leyou.common.constants.MQConstants.RoutingKey;
import com.leyou.search.service.SearchService;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import static com.leyou.common.constants.MQConstants.Exchange.ITEM_EXCHANGE_NAME;
import static com.leyou.common.constants.MQConstants.Queue.SEARCH_ITEM_DOWN;
import static com.leyou.common.constants.MQConstants.Queue.SEARCH_ITEM_UP;

@Component
public class SearchListener {
    @Autowired
   SearchService searchService;


    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value =SEARCH_ITEM_UP,declare = "true"),//持久化
                                        //↓该交换机用的RoutingKey就是ITEM_UP_KEY
            exchange = @Exchange(value = ITEM_EXCHANGE_NAME,type = ExchangeTypes.TOPIC),//默认持久化
            key = RoutingKey.ITEM_UP_KEY
    ))
    public void addItem(Long id) {
        searchService.saveItem(id);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value =SEARCH_ITEM_DOWN,declare = "true"),//持久化
            //↓该交换机用的RoutingKey就是ITEM_UP_KEY
            exchange = @Exchange(value = ITEM_EXCHANGE_NAME,type = ExchangeTypes.TOPIC),//默认持久化
            key = RoutingKey.ITEM_DOWN_KEY
    ))
    public void deleteItem(Long id) {
        searchService.deleteItem(id);
    }
}
